package com.linkcard.single.persistence;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.linkcard.single.model.SinglePage;

public class SinglePageDaoImpl extends HibernateDaoSupport implements
		SinglePageDao {

	// 添加单面
	public boolean addSinglePage(SinglePage singlePage) {
		try {
			this.getHibernateTemplate().save(singlePage);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	// 获得单面列表
	public List singlePageList(final int columnId) {
		List list = (List) this.getHibernateTemplate().execute(
				new HibernateCallback() {

					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Criteria criteria = session
								.createCriteria(SinglePage.class);
						criteria.add(Expression.eq("columnId", columnId));
						criteria.addOrder(Order.asc("sort"));
						return criteria.list();
					}

				});
		return list;
	}

	// 查找单页面
	public SinglePage findSinglePage(Integer id) {
		SinglePage singlePage = (SinglePage) this.getHibernateTemplate().get(
				SinglePage.class, id);
		return singlePage;
	}

	// 修改单页面
	public boolean modifySinglePage(SinglePage singlePage) {
		try
		{
		SinglePage sp=(SinglePage)this.getHibernateTemplate().get(SinglePage.class,singlePage.getId());
		BeanUtils.copyProperties(sp, singlePage);
		this.getHibernateTemplate().update(sp);
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		return true;
	}

	//删除页面
	public boolean deleteSinglePage(Integer id) {
		try
		{
			SinglePage singlePage=(SinglePage)this.getHibernateTemplate().get(SinglePage.class, id);
			this.getHibernateTemplate().delete(singlePage);
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		return true;
	}

}
